package com.keydom.ropeskipping;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.clj.fastble.data.BleDevice;
import com.keydom.ropeskipping.adapter.CommonAdapter;
import com.keydom.ropeskipping.adapter.CommonViewHolder;
import com.keydom.ropeskipping.dot.StudentHomeworkDTO;

import java.util.ArrayList;
import java.util.List;

public class SecondFragment extends Fragment {
    private ListView listView;
    private LinearLayout nullLayout;

    private CommonAdapter<StudentHomeworkDTO> mAdapter;
    private List<StudentHomeworkDTO> studentHomeworkDTOS = new ArrayList<>();

    public void setStudentHomeworkDTOS(final List<StudentHomeworkDTO> list) {
        if (list.size() > 0) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    studentHomeworkDTOS.clear();
                    studentHomeworkDTOS.addAll(list);
                    nullLayout.setVisibility(View.GONE);
                    mAdapter = new CommonAdapter<StudentHomeworkDTO>(getContext(), R.layout.list_second_item) {
                        @SuppressLint("SetTextI18n")
                        @Override
                        protected void convertView(View item, final StudentHomeworkDTO studentHomeworkDTO) {
                            TextView tvName = CommonViewHolder.get(item, R.id.textName);
                            TextView tvTask = CommonViewHolder.get(item, R.id.textTask);
                            ImageView imageType = CommonViewHolder.get(item, R.id.imageType);
                            tvName.setText(studentHomeworkDTO.getHomeworkName());
                            if (studentHomeworkDTO.getType() == 0) {
                                //计时计数
                                imageType.setImageResource(R.mipmap.icon_second_jishijishu);
                            } else if (studentHomeworkDTO.getType() == 1) {
                                //计时
                                imageType.setImageResource(R.mipmap.icon_second_jishi);
                            } else {
                                //计数
                                imageType.setImageResource(R.mipmap.icon_second_jishu);
                            }
                            tvTask.setText("时间：" + studentHomeworkDTO.getCompletedTime() + "s   " +
                                    "成绩：" + studentHomeworkDTO.getCompletedNum() + "个");
                        }
                    };
                    mAdapter.setDatas(studentHomeworkDTOS);
                    listView.setAdapter(mAdapter);
                }
            });
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second, container, false);
        listView = view.findViewById(R.id.listView);
        nullLayout = view.findViewById(R.id.null_layout);
        return view;
    }
}
